package com.starry.core.utils

import android.content.Context
import android.content.SharedPreferences
import android.text.TextUtils
import com.starry.core.App


/**
 * Package:com.starry.dotpen_patients.utils
 * Author:starr
 * Time:2024/1/3 20:04
 * Description:
 */
object SpUtil {
    private var mSharedPreferences: SharedPreferences =
        App.mContext.getSharedPreferences("SharedPreferences", Context.MODE_PRIVATE)


    /**
     * 保存一个字符串
     */
    fun setStringValue(key: String?, value: String?) {
        val editor = mSharedPreferences.edit()
        editor.putString(key, value)
        editor.apply()
    }

    /**
     * 获取一个字符串
     */
    fun getStringValue(key: String?): String? {
        return mSharedPreferences.getString(key, "")
    }

    /**
     * 保存多个字符串
     */
    fun setMultiStringValue(pairs: Map<String, String>?) {
        if (pairs == null || pairs.size == 0) {
            return
        }
        val editor = mSharedPreferences.edit()
        for (entry: Map.Entry<String, String> in pairs.entries) {
            val key = entry.key
            val value = entry.value
            if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)) {
                editor.putString(key, value)
            }
        }
        editor.apply()
    }

    /**
     * 获取多个字符串
     */
    fun getMultiStringValue(vararg keys: String?): Array<String?>? {
        if (keys.isEmpty()) {
            return null
        }
        val length = keys.size
        val result = arrayOfNulls<String>(length)
        for (i in 0 until length) {
            var temp: String? = ""
            if (!TextUtils.isEmpty(keys[i])) {
                temp = mSharedPreferences.getString(keys[i], "")
            }
            result[i] = temp
        }
        return result
    }


    /**
     * 保存一个布尔值
     */
    fun setBooleanValue(key: String?, value: Boolean) {
        val editor = mSharedPreferences.edit()
        editor.putBoolean(key, value)
        editor.apply()
    }

    /**
     * 获取一个布尔值
     */
    fun getBooleanValue(key: String?): Boolean {
        return mSharedPreferences.getBoolean(key, false)
    }

    fun getBooleanValue(key: String?, defaultValue: Boolean): Boolean {
        return mSharedPreferences.getBoolean(key, defaultValue)
    }

    /**
     * 保存多个布尔值
     */
    fun setMultiBooleanValue(pairs: Map<String, Boolean>?) {
        if (pairs.isNullOrEmpty()) {
            return
        }
        val editor = mSharedPreferences.edit()
        for (entry: Map.Entry<String, Boolean> in pairs.entries) {
            val key = entry.key
            val value = entry.value
            if (!TextUtils.isEmpty(key)) {
                editor.putBoolean(key, value)
            }
        }
        editor.apply()
    }

    /**
     * 获取多个布尔值
     */
    fun getMultiBooleanValue(keys: Array<String?>?): BooleanArray? {
        if (keys == null || keys.size == 0) {
            return null
        }
        val length = keys.size
        val result = BooleanArray(length)
        for (i in 0 until length) {
            var temp = false
            if (!TextUtils.isEmpty(keys[i])) {
                temp = mSharedPreferences.getBoolean(keys[i], false)
            }
            result[i] = temp
        }
        return result
    }


    fun setFirstTime(key: String?, value: Boolean) {
        val editor = mSharedPreferences.edit()
        editor.putBoolean(key, value)
        editor.apply()
    }

    /**
     * 是否第一次启动
     *
     * @param key
     * @return
     */
    fun isFirstTime(key: String?): Boolean {
        return mSharedPreferences.getBoolean(key, true)
    }


    fun removeValue(vararg keys: String?) {
        val editor = mSharedPreferences.edit()
        for (key in keys) {
            editor.remove(key)
        }
        editor.apply()
    }


    /**
     * 存储int
     *
     * @param key
     * @param value
     */
    fun setIntValue(key: String?, value: Int) {
        val editor = mSharedPreferences.edit()
        editor.putInt(key, value)
        editor.apply()
    }

    /**
     * 取出int
     *
     * @param key
     * @return
     */
    fun getIntValue(key: String?): Int {
        return getIntValue(key, -1)
    }

    /**
     * 取出int
     *
     * @param key
     * @return
     */
    fun getIntValue(key: String?, defaultValue: Int): Int {
        return mSharedPreferences.getInt(key, defaultValue)
    }

    /**
     * 存储long
     *
     * @param key
     * @param value
     */
    fun setLongValue(key: String?, value: Long) {
        val editor = mSharedPreferences.edit()
        editor.putLong(key, value)
        editor.apply()
    }

    /**
     * 取出long
     *
     * @param key
     * @return
     */
    fun getLongValue(key: String?): Long {
        return mSharedPreferences.getLong(key, 0)
    }

    /**
     * 存储Double
     *
     * @param key
     * @param value
     */
    fun setDoubleValue(key: String?, value: Double) {
        val editor = mSharedPreferences.edit()
        editor.putString(key, value.toString())
        editor.apply()
    }

    /**
     * 取出long
     *
     * @param key
     * @return
     */
    fun getDoubleValue(key: String?): Number {
        val value = mSharedPreferences.getString(key, "0")
        return value?.toDouble() ?: 0
    }

    /**
     * 清除
     */
    fun clear() {
        mSharedPreferences.edit().clear().apply()
    }
}